package de.kalinasch.pa.placereminder.database;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import de.kalinasch.pa.placereminder.dmo.IncidentDMO;
import de.kalinasch.pa.placereminder.dmo.IncidentListItemDMO;
import de.kalinasch.pa.placereminder.dmo.PositionDMO;

public class IncidentDataSource implements I_Incident {

	DBManager dbman;
	SQLiteDatabase db;

	public IncidentDataSource(Context _context) {
		dbman = new DBManager(_context);
	}

	/**
	 * Saves the data of the incident that are defined by the user.
	 * 
	 * @param _incident
	 *            all data that describes the incident
	 * @return id the new id of the incident
	 */
	public long saveIncident(IncidentDMO _incident) {
		long id = 0;
		open();

		ContentValues values = new ContentValues();
		values.put(STREET, _incident.getStreet());
		values.put(TOWN, _incident.getTown());
		values.put(RADIUS, _incident.getRadius());
		values.put(TITLE, _incident.getTitle());
		values.put(DESCRIPTION, _incident.getDescription());
		values.put(LONGITUDE, _incident.getLongitude());
		values.put(LATITUDE, _incident.getLatitude());

		id = db.insert(TABLE_NAME, null, values);

		close();
		return id;
	}

	/**
	 * Reads the required data of all saved incident to display them in a list
	 * 
	 * @return
	 */
	public ArrayList<IncidentListItemDMO> listAllIncidents() {
		open();
		String[] columns = new String[] { ID, TITLE, DESCRIPTION };

		Cursor result = db.query(TABLE_NAME, columns, null, null, null, null, null);
		ArrayList<IncidentListItemDMO> incidentlist = new ArrayList<IncidentListItemDMO>();
		if (result.getCount() > 0) {
			while (result.moveToNext()) {
				incidentlist.add(ResultConverter.createIncidtentListItemDMO(result));
			}
		}
		result.close();
		close();
		return incidentlist;
	}

	/**
	 * Loads all data of an incident for the given id.
	 * 
	 * @param _id
	 *            the id of the incident that should be loaded
	 * @return the IncidentDMO with the given id
	 */
	public IncidentDMO getIncident(int _id) {
		open();

		String[] columns = new String[] { ID, TITLE, DESCRIPTION, STREET, TOWN, RADIUS, LONGITUDE, LATITUDE };
		String selection = ID + " = " + _id;

		Cursor cursor = db.query(TABLE_NAME, columns, selection, null, null, null, null);

		IncidentDMO result = new IncidentDMO();
		if (cursor.getCount() > 0) {
			if (cursor.moveToNext()) {
				result = ResultConverter.createIncident(cursor);
			}
		}
		close();
		return result;
	}

	/**
	 * Deletes a incident selected by the user
	 * 
	 * @param _id
	 * 		the id of the incident that should be deleted
	 */
	public void deleteIncident(int _id) {
		open();
		String selection = ID + " = " + _id;
		db.delete(TABLE_NAME, selection, null);
		close();
	}
	
	/**
	 * The background service needs all saved position data to compare them with the current position.
	 * @return all saved position data
	 */
	public ArrayList<PositionDMO> getAllPositionDMO() {
		ArrayList<PositionDMO> positionList = new ArrayList<PositionDMO>();
		
		open();
		String[] columns = new String[] { ID, TITLE, DESCRIPTION, LONGITUDE, LATITUDE, RADIUS };
		Cursor cursor = db.query(TABLE_NAME, columns, null, null, null, null,
					null);
		
		if (cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				positionList.add(ResultConverter.createPositionDMO(cursor));
			}
		}

		close();
		return positionList;
	}

	/**
	 * Updates the data of an incident, after the user changed it.
	 * 
	 * @param _incident
	 * 			contains the data that should be saved
	 */
	public void updateIncident(IncidentDMO _incident) {
		open();
		String selection = ID + " = " + _incident.getId();
		ContentValues werte = new ContentValues();
		werte.put(TITLE, _incident.getTitle());
		werte.put(DESCRIPTION, _incident.getDescription());
		werte.put(STREET, _incident.getStreet());
		werte.put(TOWN, _incident.getTown());
		werte.put(RADIUS, _incident.getRadius());
		werte.put(LONGITUDE, _incident.getLongitude());
		werte.put(LATITUDE, _incident.getLatitude());

		db.update(TABLE_NAME, werte, selection, null);
		close();
	}
	
	/**
	 * Open connection to the databasse
	 * 
	 */
	private void open() {
		db = dbman.getWritableDatabase();
	}

	/**
	 * Close connection to the database
	 * 
	 */
	private void close() {
		db.close();
	}
}
